/*
* Copyright 2024 NXP
* NXP Confidential and Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include <stdio.h>
#include "lvgl.h"
#include "events_init.h"
#include "dlight_bh1750.h"
#include "meter.h"
#include "custom.h"

#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif


static void screen_lightmeter_lbl_lux_value_event_handler(lv_event_t* e)
{
	lv_event_code_t code = lv_event_get_code(e);
	switch (code) {
	case LV_EVENT_REFRESH:
	{
		double lux = bh1750_get_lux();
		lv_label_set_text_fmt(guider_ui.screen_lightmeter_lbl_lux_value, "%d.%d", ((int)(lux * 100)) / 100, ((int)(lux * 100)) % 100);
		lv_obj_invalidate(guider_ui.screen_lightmeter_lbl_lux_value); // 强制重新绘制 UI

		double ev = calc_ev_by_lux(lux);
		LV_LOG_USER("\r\n>ev: %d.%d", (int)ev, ((int)(ev * 100)) % 100);
		lv_label_set_text_fmt(guider_ui.screen_lightmeter_lbl_ev_value, "%d.%d", (int)ev, ((int)(ev * 100)) % 100);

		custom_meter_update_widgets(&guider_ui);

		break;
	}

	default:
		break;
	}
}

static void screen_welcome_btn_next_scr_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_lightmeter, guider_ui.screen_lightmeter_del, &guider_ui.screen_welcome_del, setup_scr_screen_lightmeter, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 200, false, true);
		break;
	}
    default:
        break;
    }
}

void events_init_screen_welcome (lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_welcome_btn_next_scr, screen_welcome_btn_next_scr_event_handler, LV_EVENT_ALL, ui);
}

static void screen_lightmeter_btn_prev_scr_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		ui_load_scr_animation(&guider_ui, &guider_ui.screen_welcome, guider_ui.screen_welcome_del, &guider_ui.screen_lightmeter_del, setup_scr_screen_welcome, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 200, false, true);
		break;
	}
    default:
        break;
    }
}

static void screen_lightmeter_btn_mode_value_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED: {
		if (METER_MODE_AV == meter_mode_get()) {
			// 切换到 TV -- 快门优先模式
			lv_label_set_text(guider_ui.screen_lightmeter_btn_mode_value_label, "TV");
		} else {
			// 切换到 AV -- 光圈优先模式
			lv_label_set_text(guider_ui.screen_lightmeter_btn_mode_value_label, "AV");
		}

		meter_mode_switch(); // 切换模式
		custom_meter_update_mode_widgets(&guider_ui);

		break;
	}

	default:
        break;
    }
}

static void screen_lightmeter_btn_iso_minus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		// TODO: 开机时显示的 ISO 档位正确吗？

		// 先更新数值
		iso_idx_decrement();

		// 再更新显示
		lv_label_set_text(guider_ui.screen_lightmeter_lbl_iso_value, (const char*)iso_get_string());

		break;
	}
    default:
        break;
    }
}

static void screen_lightmeter_btn_iso_plus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		// 先更新档位
		iso_idx_increment();

		// 再更新显示
		lv_label_set_text(guider_ui.screen_lightmeter_lbl_iso_value, (const char*)iso_get_string());
		break;
	}
    default:
        break;
    }
}

static void screen_lightmeter_btn_ap_minus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		// AV 光圈索引更新
		av_idx_decrement();

		// 更新 GUI
		lv_label_set_text(guider_ui.screen_lightmeter_lbl_ap_value, (const char*)av_get_string());
		break;
	}
    default:
        break;
    }
}

static void screen_lightmeter_btn_ap_plus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		// AV 光圈索引更新
		av_idx_increment();

		// 更新 GUI
		lv_label_set_text(guider_ui.screen_lightmeter_lbl_ap_value, (const char*)av_get_string());
		break;
	}
    default:
        break;
    }
}

static void screen_lightmeter_btn_sp_minus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		// 快门档位减
		tv_idx_decrement();

		// 更新快门 GUI
		lv_label_set_text(guider_ui.screen_lightmeter_lbl_sp_value, (const char*)tv_get_string());
		break;
	}
    default:
        break;
    }
}

static void screen_lightmeter_btn_sp_plus_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
	case LV_EVENT_CLICKED:
	{
		// 快门档位加
		tv_idx_increment();

		// 更新快门 GUI
		lv_label_set_text(guider_ui.screen_lightmeter_lbl_sp_value, (const char*)tv_get_string());
		break;
	}
    default:
        break;
    }
}

void events_init_screen_lightmeter (lv_ui *ui)
{
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_prev_scr, screen_lightmeter_btn_prev_scr_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_mode_value, screen_lightmeter_btn_mode_value_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_iso_minus, screen_lightmeter_btn_iso_minus_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_iso_plus, screen_lightmeter_btn_iso_plus_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_ap_minus, screen_lightmeter_btn_ap_minus_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_ap_plus, screen_lightmeter_btn_ap_plus_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_sp_minus, screen_lightmeter_btn_sp_minus_event_handler, LV_EVENT_ALL, ui);
	lv_obj_add_event_cb(ui->screen_lightmeter_btn_sp_plus, screen_lightmeter_btn_sp_plus_event_handler, LV_EVENT_ALL, ui);

	if (ui->screen_lightmeter_lbl_lux_value) {
		lv_obj_add_event_cb(ui->screen_lightmeter_lbl_lux_value, screen_lightmeter_lbl_lux_value_event_handler, LV_EVENT_REFRESH, ui);
	}

	custom_meter_update_mode_widgets(ui);
}

void events_init(lv_ui *ui)
{

}
